package com.peng.leetcode.list;

/**
 * DeleteDuplicates
 * 82. 删除排序链表中的重复元素 II
 * <p>
 * 给定一个排序链表，删除所有含有重复数字的节点，只保留原始链表中 没有重复出现 的数字
 *
 * @author: lupeng6
 * @create: 2021/1/13 20:12
 */
public class DeleteDuplicates {

    public static void main(String[] args) {
        ListNode listNode = BuildListNode.tailInsert(1, 2, 3, 3, 4, 4, 5);
        ListNode node = new DeleteDuplicates().deleteDuplicates(listNode);
        PrintListNode.print(node);
    }

    public ListNode deleteDuplicates(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode prev = dummy;

        Integer remove = null;
        while (head != null) {
            if (head.next != null) {
                if (head.val == head.next.val) {
                    remove = head.val;
                }
            }
            if (remove != null && remove == head.val) {
                prev.next = head.next;
            } else {
                prev = head;
            }
            head = head.next;
        }

        return dummy.next;
    }
}
